package scu.maqiang.cte;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import scu.maqiang.fes.BVPType;
import scu.maqiang.fes.FES1L21;
import scu.maqiang.fes.FES1L2CTE;
import scu.maqiang.mesh.Mesh1L2;
import scu.maqiang.mesh.Tecplot;
import scu.maqiang.numeric.Direct;
import scu.maqiang.numeric.MVO;
import scu.maqiang.numeric.NewIterSSolver;
import scu.maqiang.numeric.SRMatrix;

public class RectangleResultL2_DC1 {

	public static void main(String[] args) {
		double kxx = 1.0, density = 1.0, specificHeat = 1.0; 
	    double E = 2.0, Nu = 0.3, alpha = 0.05;
	    double beta = E / (1 - 2 * Nu)  * alpha;
	    
	    double theta = 0.5, T = 1.0e1, dt = T / 1000;
	    
	    int lineNum = 1000;
	    Mesh1L2 mesh1D = new Mesh1L2();
		mesh1D.line(0, 1.0, lineNum);
	    
	    FES1L21 ffs = new FES1L21(mesh1D);
	    
	    FES1L2CTE fs = new FES1L2CTE(mesh1D, ffs);
	    
	    SRMatrix L = new SRMatrix(fs.GetNdof());
	    SRMatrix R = new SRMatrix(fs.GetNdof());
	    
	    double[] x = new double[fs.GetNdof()];
	    double[] RHS = x.clone();
	    
	    double[] temp = new double[fs.GetNdof() / 2];
	    double[][] dispUV = new double[1][ffs.GetNdof()];
	    double[] disp = new double[ffs.GetNdof()];
	    
	    double[] ex = temp.clone();
	    double[] ey = temp.clone();
	    double[] exy = temp.clone();
	    double[] ez = temp.clone();
	    double[] tx = temp.clone();
	    double[] ty = temp.clone();
	    double[] txy = temp.clone();
	    
	    double MM = E * (1 - Nu) / ((1 + Nu) * (1 - 2 * Nu));
	    double TRef = 8.0 * 35.0 / 13.0;
	    double AA = MM + TRef * beta * beta / (density * specificHeat);
	    
	    double[] coef = {density, specificHeat, kxx, TRef, theta, E, Nu, beta, dt};
	    fs.assembleDierctCouplingMatrix_Type1(coef, BVPType.COMMON, BVPType.COMMON,
	    		                              BVPType.CONSIST_MASS, BVPType.COMMON, L, R);
	    L.sort();
//	    System.out.println(R);
//	    System.exit(0);

	    fs.applyBCHeat_MBN(L, 2);
	    fs.applyBCDisp_MBN(L, Direct.All, 1);
//	    System.out.println(L);
//	    System.exit(0);
	    NewIterSSolver solver = new NewIterSSolver(L);
	    //IterSSolver solver = new IterSSolver();
	    //DirectSSolver solver = new DirectSSolver(L);
	    
	    double time = 0.0;
	   
	    double dis = 1.0 / lineNum;
	    double[] lineX = new double[lineNum + 1];
	    double[] lineY = lineX.clone();
	    
	    Arrays.setAll(lineX, i -> i * dis);
	    String dir = "RectangleResultL2\\";
	    String XYLineFile = dir + "Line150.dat";
	    Tecplot.lineXYTime(XYLineFile, lineX, lineY, 0.0);
	    
	    String heatFile = dir + "Heat150.dat";
	    mesh1D.toTecplot(heatFile, 0.0, x);
	    
	    String deformFile =  dir + "Deform150.dat";
	    mesh1D.toTecplot(deformFile, 0.0, dispUV[0]);
	    
	    String expansionFile = dir + "expansion150.dat";
	    String temp00File = dir + "temp00_150.dat";
	    FileWriter fw1 = null;
	    FileWriter fw2 = null;
	    try {
			fw1 = new FileWriter(expansionFile);
			fw2 = new FileWriter(temp00File);
			fw1.write("0.0\t0.0\n");
			fw2.write("0.0\t0.0\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    while(time < T) {
	    	time += dt;
	    	System.out.println("time: " + time);
	    	fs.applyBCHeat_MBN(RHS, 1.0, 2);
		    fs.applyBCDisp_MBN(RHS, Direct.All, 0.0, 1);
		    //solver.GMRES(RHS, x, 30);
		    //solver.CGS(L, RHS, x);
		    //solver.CGNR(L, RHS, x);
		    //solver.BiCGSTAB(L, RHS, x);
		    //solver.PCGSSSOR(L, RHS, x, 1.5);
		    solver.PCGSSSOR(RHS, x, 1.5, 1);
		    //solver.PPLUSolve(RHS, x, 1);
		    fs.extractUVH(x, temp, dispUV);
		    System.out.println("T min: " + MVO.min(temp)  + "\t max: " + MVO.max(temp));
		    System.out.println("U min: " + MVO.min(dispUV[0]) + "\t max: " + MVO.max(dispUV[0]));
		    R.mul(x, RHS);
		    
		    mesh1D.toTecplot(heatFile, time, temp);
		    Arrays.setAll(disp, i -> dispUV[0][i] * (1-Nu) / (alpha * (1 + Nu)));
		    mesh1D.toTecplot(deformFile, time, disp);
	        Tecplot.lineXYTime(XYLineFile, lineX, temp, time);
	        try {
	        	fw1.write(time + "\t " + dispUV[0][10] * (1-Nu)/(alpha * (1 + Nu))  + "\n");
				fw2.write(time + "\t " + temp[0] + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    try {
	    	fw1.flush();
			fw1.close();
			fw2.flush();
			fw2.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
